/**
 * BillDao.java
 * com.jamin.aabill.dao
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2012-9-3 		wangjieming
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */

package com.jamin.aabill.dao;

import java.text.DecimalFormat;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jamin.aabill.data.Bill;
import com.jamin.aabill.data.BillDetail;
import com.jamin.aabill.data.Circle;
import com.jamin.aabill.db.BillDataBaseHelper;
import com.jamin.aabill.db.BillDetailTableMetaData;
import com.jamin.aabill.db.BillTableMetaData;
import com.jamin.aabill.db.BillViewMetaData;
import com.jamin.aabill.db.CircleTableMetaData;
import com.jamin.aabill.ui.AABillApplication;
import com.jamin.aabill.utils.Utils;

/**
 * ClassName:BillDao
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-9-3 下午8:59:22 
 */
public class BillAndDetailDao {



	private static final String TAG = "BillDao";
	BillDataBaseHelper mDbHelper = null;
	long id = -1;

	public BillAndDetailDao(){
		mDbHelper = AABillApplication.mDB;
	}


	/**
	 * 插入账单<br>
	 * 用事物进行两个表的同时插入<br>
	 * @method insert
	 * @param bill
	 * @param billDetailList
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public synchronized long insert(Bill bill , List<BillDetail> billDetailList){
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		try{
			db.beginTransaction();
			id = db.insert(BillTableMetaData.TABLE_NAME, null, getBillContentValues(bill));
			for(int i = 0 ; i < billDetailList.size() ; i++){
				db.insert(BillDetailTableMetaData.TABLE_NAME, null, getBillDetailContentValues(billDetailList.get(i)));
			}
			db.setTransactionSuccessful();
		}finally{
			db.endTransaction();
		}
		return id;
	}

	/**
	 * 查询账单<br>
	 * @method getAllBillDetail
	 * @param where
	 * @param order
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public Cursor getAllBillDetail(String where , String order){
		SQLiteDatabase db = mDbHelper.getReadableDatabase();
		return db.query(BillViewMetaData.VIEW_NAME, null, where, null, null, null, order);
	}


	/**
	 * 得到BillList的游标<br>
	 * @method getBillList
	 * @param circleId
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public Cursor getBillList(long circleId){
		SQLiteDatabase db = mDbHelper.getReadableDatabase();
		return db.query(BillTableMetaData.TABLE_NAME, null, BillTableMetaData.CIRCLE_ID + "=" + circleId, null, null, null, BillTableMetaData.CREATE_AT + " desc");
	}




	/**
	 * 查询Bill的Money字段<br>
	 * @method getTotalCost
	 * @param where
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public synchronized double getTotalCost(String  where){
		double sum = 0;
		SQLiteDatabase db = mDbHelper.getReadableDatabase();
		Cursor cur = db.query( BillViewMetaData.VIEW_NAME,
				new String []{ BillViewMetaData.MONEY }, 
				where , null , null , null , null );
		if(cur!=null && cur.getCount()!=0){
			for(cur.moveToFirst(); !cur.isAfterLast() ; cur.moveToNext()){
				sum += cur.getDouble(cur.getColumnIndex( BillViewMetaData.MONEY ));
			}
			cur.close();
			DecimalFormat df = new DecimalFormat("0.000");
			return Double.valueOf(df.format(sum));
		}else{
			return sum;
		}

	}



	public void deleteBill(long billId){
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		db.beginTransaction();
		try{
			db.delete(BillDetailTableMetaData.TABLE_NAME ,BillDetailTableMetaData.BILL_ID + "=" + billId, null);
			db.delete(BillTableMetaData.TABLE_NAME ,BillTableMetaData._ID + "=" + billId, null);
			db.setTransactionSuccessful();
		}finally{
			db.endTransaction();
		}
	}



	private ContentValues getBillDetailContentValues(BillDetail billDetail) {
		ContentValues values = new ContentValues();
		values.put(BillDetailTableMetaData.BILL_ID, id);
		values.put(BillDetailTableMetaData.CIRCLE_MEMBER_ID, billDetail.getCIRCLE_MEMBER_ID());
		values.put(BillDetailTableMetaData.MONEY, billDetail.getMONEY());
		return values;

	}



	private ContentValues getBillContentValues(Bill bill){
		ContentValues values = new ContentValues();
		values.put(BillTableMetaData.PROJECT, bill.getPROJECT());
		values.put(BillTableMetaData.DESCRIPTION, bill.getDESCRIPTION());
		values.put(BillTableMetaData.CIRCLE_ID, bill.getCIRCLE_ID());
		values.put(BillTableMetaData.TYPE, bill.getTYPE());
		values.put(BillTableMetaData.CREATOR_MEMBER_ID, 0);
		values.put(BillTableMetaData.CREATE_AT, bill.getCREATE_AT());
		if(bill.getCREATE_AT() != 0){
			values.put(BillTableMetaData.MODIFY_AT, bill.getMODIFY_AT());
		}
		return values;
	}



}

